package com.eeepay.eeepay_v2.api;

import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.util.GrpcUtils;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import cn.eeepay.api.grpc.nano.TerminalApiServiceGrpc;
import cn.eeepay.api.grpc.nano.TerminalSerProto;
import io.grpc.ManagedChannel;

/**
 * 描述：获取所有活动信息
 * 作者：zhuangzeqin
 * 时间: 2017/8/21-16:11
 * 邮箱：zzq@eeepay.cn
 */
public class GetAllActivityInfoAPI {
    private ResultCallBack resultCallBack;//设置监听接口
    /**
     * 设置监听接口
     *
     * @param resultCallBack
     */
    public void setResultCallBack(ResultCallBack resultCallBack) {
        this.resultCallBack = resultCallBack;
    }

    /**
     * 获取所有活动信息
     */
    public void getAllActivityInfo()
    {
        if (resultCallBack == null) {
            throw new RuntimeException("ResultCallBack is null,you can must implement.");
        }
        GrpcUtils.getInstance().doGrpcTask(Constant.URL.URL, Constant.URL.PORT, 0, new GrpcUtils.AsyncTaskListener() {
            @Override
            public Object doInBack(ManagedChannel channel, int taskId) {
                TerminalApiServiceGrpc.TerminalApiServiceBlockingStub stub = TerminalApiServiceGrpc
                        .newBlockingStub(channel).withDeadlineAfter(30, TimeUnit.SECONDS);
                TerminalSerProto.AgentInfoMsg agentInfoMsg = new TerminalSerProto.AgentInfoMsg();
                //不带参数查询；全部活动
                agentInfoMsg.agentNo ="";//调用查询的用户ID，代理商传代理商编号
                agentInfoMsg.agentName = "";
                agentInfoMsg.agentNode = "";
                return stub.getAllActivityInfo(agentInfoMsg);
            }

            @Override
            public void updateUI(Object result, int taskId) {
                if (result == null) {
                    resultCallBack.onFail("机具活动类型获取失败，请稍后重试！");
                    return;
                }
                TerminalSerProto.ActivityInfoRes res = (TerminalSerProto.ActivityInfoRes)result;
                if ("false".equals(res.resMsg.status)) {
                    resultCallBack.onFail(res.resMsg.msg);
                    return;
                }
                TerminalSerProto.ActivityInfo[] activityInfos = res.activityInfos;
                resultCallBack.onSucess(Arrays.asList(activityInfos));
            }
        });
    }
    /**
     * 提供外围接口；通过接口回传
     */
    public interface ResultCallBack {
        void onFail(String msg);//失败返回；提示语

        void onSucess(List<TerminalSerProto.ActivityInfo> activityInfos);//成功返回列表数据数据
    }
}
